package com.kindsoft.demo.ad;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import org.springframework.ldap.support.LdapNameBuilder;

import javax.naming.Name;
import java.util.List;

/**
 * Created by zhoujm on 2020/2/20/020.
 */
@Data
@Entry(objectClasses = {"top","person","organizationalPerson","user"}, base="dc=unicademo,dc=com")
public class Person {

    @Id
    @JsonIgnore
    private Name dn;   // CN=steve,CN=Users
    @Attribute(name="sAMAccountName")
    private String name; // xinghouliu
    @Attribute(name="userPrincipalName")
    private String principalName; // steve@example.com
    @Attribute(name="memberOf")
    private List<String> memberOf; // CN=Senior Developers,CN=Managed Service Accounts,DC=datamesh,DC=com

    public Person() {
    }

    public Person(String name) {
        this.dn = LdapNameBuilder.newInstance().add("cn", "Users").add("cn", name).build();
    }

    public void setName(String name) {
        this.name = name;
        if(this.dn == null){
            this.dn = LdapNameBuilder.newInstance().add("cn", "Users").add("cn", name).build();
        }
    }

    public Name getDn() {
        return dn;
    }

    public void setDn(Name dn) {
        this.dn = dn;
    }

    public String getName() {
        return name;
    }

    public String getPrincipalName() {
        return principalName;
    }

    public void setPrincipalName(String principalName) {
        this.principalName = principalName;
    }

    public List<String> getMemberOf() {
        return memberOf;
    }

    public void setMemberOf(List<String> memberOf) {
        this.memberOf = memberOf;
    }

    @Override
    public String toString() {
        return "Person{" +
                "dn=" + dn +
                ", name='" + name + '\'' +
                ", principalName='" + principalName + '\'' +
                ", memberOf=" + memberOf +
                '}';
    }
}
